package org.bizming.web.action;

import java.util.List;

import org.bizming.base.BaseErrCode;
import org.bizming.medicine.model.ext.PurChaseExt;
import org.bizming.medicine.model.help.MedicineStatus;
import org.bizming.medicine.model.help.MedicineUnit;
import org.bizming.medicine.model.help.OfferCompany;
import org.bizming.medicine.service.MedicineStatusService;
import org.bizming.medicine.service.MedicineUnitService;
import org.bizming.medicine.service.OfferCompanyService;
import org.bizming.medicine.service.PurchaseService;
import org.bizming.web.util.WebUtil;

public class PurchaseAction {

	private PurchaseService purchaseService;
	private OfferCompanyService companyService;
	private MedicineUnitService unitService;
	private MedicineStatusService statusService;

	private PurChaseExt t;

	public String addOrUpdate() {
		int errCode = BaseErrCode.SUCCESS;

		try {
			if (null != t) {
				if (t.getMedicine() != null) {
					t.getMedicine().setRemainder(t.getAmount());
				}
				t.setInTime(System.currentTimeMillis() / 1000);
				t.setValiTime(System.currentTimeMillis() / 1000);
				purchaseService.add(t);
			}

			WebUtil.getResponse().sendRedirect(
					WebUtil.getRequest().getContextPath() + "/purchase!selectList.action");
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			errCode = BaseErrCode.SERVER_ERROR;
		}
		WebUtil.getRequest().setAttribute("code", errCode);
		WebUtil.getRequest().setAttribute("o", t);
		return "addOrUpdate_exception";
	}

	public String view() {
		PurChaseExt ext = null;
		if (t != null && t.getId() != null) {
			ext = purchaseService.get(t.getId());
		}
		List<OfferCompany> companies = companyService.selectList();
		List<MedicineUnit> units = unitService.selectList();
		List<MedicineStatus> statusList = statusService.selectList();

		WebUtil.getRequest().setAttribute("t", ext);
		WebUtil.getRequest().setAttribute("companylist", companies);
		WebUtil.getRequest().setAttribute("unitlist", units);
		WebUtil.getRequest().setAttribute("statuslist", statusList);
		return "view";
	}

	// @Deprecated
	// public String remove() throws IOException {
	// if (ext != null && ext.getPurchase() != null && ext.getPurchase().getId()
	// != null) {
	// purchaseService.remove(ext.getPurchase().getId());
	// }
	// WebUtil.getResponse().sendRedirect("/purchase!selectList.action");
	// return null;
	// }
	public String selectList() {
		List<PurChaseExt> list = purchaseService.selectList();
		WebUtil.getRequest().setAttribute("list", list);

		return "selectList";
	}

	// =======================================
	public PurchaseService getPurchaseService() {
		return purchaseService;
	}

	public void setPurchaseService(PurchaseService purchaseService) {
		this.purchaseService = purchaseService;
	}

	public PurChaseExt getT() {
		return t;
	}

	public void setT(PurChaseExt t) {
		this.t = t;
	}

	public OfferCompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(OfferCompanyService companyService) {
		this.companyService = companyService;
	}

	public MedicineUnitService getUnitService() {
		return unitService;
	}

	public void setUnitService(MedicineUnitService unitService) {
		this.unitService = unitService;
	}

	public MedicineStatusService getStatusService() {
		return statusService;
	}

	public void setStatusService(MedicineStatusService statusService) {
		this.statusService = statusService;
	}

}
